package com.example.onlinetaxi.view.Adapter;

import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.onlinetaxi.R;
import com.example.onlinetaxi.callback.OrderCallBack.CompletedOrderCallBack;
import com.example.onlinetaxi.dto.OrderDto.CompletedOrderDto;
import com.example.onlinetaxi.util.TimeUtil;

import java.util.List;

public class CompletedOrderAdapter extends RecyclerView.Adapter<CompletedOrderAdapter.ViewHolder> {
    // 用于构造页面的订单列表
    private List<CompletedOrderDto> completeOrderList;
    // 删除订单接口
    private CompletedOrderCallBack completedOrderCallBack;

    // 文本颜色
    public static Drawable drawable7;
    public static Drawable drawable8;
    // 按钮颜色
    public static Drawable drawable_pay_btn;
    public static Drawable drawable_delete_btn;

    // 构造函数
    public CompletedOrderAdapter(List<CompletedOrderDto> completeOrderList) {
        this.completeOrderList = completeOrderList;
    }

    /**
     * 内部类也继承其内部类，并重写构造器
     */
    static class ViewHolder extends RecyclerView.ViewHolder {
        // 布局
        LinearLayout completeOrderAllInfo;
        TextView completeOrderId;
        TextView completeOrderUseCarTime;
        // 订单类型（实时单/预约单）
        TextView completeOrderUseCarType;
        TextView completeOrderStartInfo;
        TextView completeOrderEndInfo;
        TextView completeOrderCarBrand;
        TextView completeOrderCarColor;
        TextView completeOrderCarNumber;
        TextView completeOrderCost;
        // 配置
        TextView completeOrderLeftColor;
        TextView completeOrderRightInfo;
        // 按钮
        Button completeOrderBtn;

        /**
         * 内部类构造器
         *
         * @param view Recycler子项的最外层布局
         */
        public ViewHolder(View view) {
            super(view);
            // 布局
            completeOrderAllInfo = view.findViewById(R.id.complete_order_all_info);  // 点击后进入等待接单界面
            // 文本框
            completeOrderId = view.findViewById(R.id.complete_order_id);
            completeOrderUseCarTime = view.findViewById(R.id.complete_order_use_car_time);
            completeOrderUseCarType = view.findViewById(R.id.complete_order_use_car_type);
            completeOrderStartInfo = view.findViewById(R.id.complete_order_start_info);
            completeOrderEndInfo = view.findViewById(R.id.complete_order_end_info);
            completeOrderCarBrand = view.findViewById(R.id.complete_order_car_brand);
            completeOrderCarColor = view.findViewById(R.id.complete_order_car_color);
            completeOrderCarNumber = view.findViewById(R.id.complete_order_car_number);
            completeOrderCost = view.findViewById(R.id.complete_order_cost);
            // 颜色配置
            completeOrderLeftColor = view.findViewById(R.id.complete_order_left_color);
            completeOrderRightInfo = view.findViewById(R.id.complete_order_info);
            // 按钮
            completeOrderBtn = view.findViewById(R.id.complete_order_pay_btn);

        }
    }

    // 第二个被回调，创建一个ViewHolder对象返回
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        // 加载布局
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.order_item_complete, parent, false);
        // 布局传递到内部类的构造函数中
        ViewHolder holder = new ViewHolder(view);
        // 返回内部类对象
        return holder;
    }

    // 子项数据赋值，通过这个方法实现滑动添加，第三个被回调
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        // 得到当前order
        CompletedOrderDto order = completeOrderList.get(position);

        holder.completeOrderAllInfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 使用回调函数跳转等待接单
                completedOrderCallBack.jump(order);
            }
        });
        // 设置订单id
        holder.completeOrderId.setText("订单id:" + order.getId());
        holder.completeOrderUseCarTime.setText("用车时间: " + TimeUtil.DateToString(order.getUseCarTime()));
        holder.completeOrderUseCarType.setText(order.getUseCarType());
        holder.completeOrderStartInfo.setText(order.getStartInfo());
        holder.completeOrderEndInfo.setText(order.getEndInfo());
        holder.completeOrderCost.setText("￥ 乘车费用" + order.getCost() + " 元");
        holder.completeOrderCarBrand.setText("车辆品牌:" + order.getCarBrand());
        holder.completeOrderCarColor.setText("车辆颜色:" + order.getCarColor());
        holder.completeOrderCarNumber.setText("车牌号:" + order.getCarNumber());
        if (order.getState().equals("7")) {
            // 待支付
            holder.completeOrderLeftColor.setBackground(drawable7);
            holder.completeOrderRightInfo.setTextColor(Color.parseColor("#8EA2F6"));
            holder.completeOrderRightInfo.setText("待支付");
            holder.completeOrderBtn.setText("支付订单");
            holder.completeOrderBtn.setBackground(drawable_pay_btn);
            // 按钮事件
            holder.completeOrderBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    completedOrderCallBack.clickPayButton(order);
                }
            });
        } else if (order.getState().equals("8")) {
            // 已完成
            holder.completeOrderLeftColor.setBackground(drawable8);
            holder.completeOrderRightInfo.setTextColor(Color.parseColor("#4BE4B0"));
            holder.completeOrderRightInfo.setText("已完成");
            holder.completeOrderBtn.setText("删除订单");
            holder.completeOrderBtn.setBackground(drawable_delete_btn);
            // 按钮事件
            holder.completeOrderBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    completedOrderCallBack.clickDeleteButton(order);
                }
            });
        }
    }

    //  @return 获取长度，第一个被回调
    public int getItemCount() {
        return completeOrderList.size();
    }

    public void setCompletedOrderCallBack(CompletedOrderCallBack completedOrderCallBack) {
        this.completedOrderCallBack = completedOrderCallBack;
    }
}
